﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace assignment7
{
    public partial class Form1 : Form
    {

        public OrderService myOrderService = new OrderService();
        private addOrderForm newAdderForm = new addOrderForm();
        private editOrdermessage neweditForm = new editOrdermessage();

        public Form1()
        {
            InitializeComponent();
            //Order exampleOrder = new Order(12138, "老王", "2003-1-1", 666.66);
            //myOrderService.order.Add(exampleOrder);
            DataGridViewColumn col1 = new DataGridViewColumn();
            DataGridViewCell cell = new DataGridViewTextBoxCell();
            col1.CellTemplate = cell;
            col1.HeaderText = "订单号";
            allOrderView.Columns.Add(col1);
            DataGridViewColumn col2 = new DataGridViewColumn();
            DataGridViewCell cel2 = new DataGridViewTextBoxCell();
            col2.CellTemplate = cell;
            col2.HeaderText = "客户";
            allOrderView.Columns.Add(col2);
            DataGridViewColumn col3 = new DataGridViewColumn();
            DataGridViewCell cel3 = new DataGridViewTextBoxCell();
            col3.CellTemplate = cel3;
            col3.HeaderText = "下单时间";
            allOrderView.Columns.Add(col3);
            DataGridViewColumn col4 = new DataGridViewColumn();
            DataGridViewCell cel4 = new DataGridViewTextBoxCell();
            col4.CellTemplate = cel4;
            col4.HeaderText = "总价";
            allOrderView.Columns.Add(col4);

            int nums_of_orders = myOrderService.order.ToArray().Length;
            for(int i = 0; i < nums_of_orders; i++)
            {
                List<string> values = new List<string>();
                values.Add(myOrderService.order[i].id.ToString());
                values.Add(myOrderService.order[i].customer);
                values.Add(myOrderService.order[i].Date);
                values.Add(myOrderService.order[i].Money.ToString());
                allOrderView.Rows.Add(values.ToArray());
            }

            DataGridViewColumn col5 = new DataGridViewColumn();
            DataGridViewCell cel5 = new DataGridViewTextBoxCell();
            col5.CellTemplate = cel5;
            col5.HeaderText = "序号";
            oneOrderDataview.Columns.Add(col5);
            DataGridViewColumn col6 = new DataGridViewColumn();
            DataGridViewCell cel6 = new DataGridViewTextBoxCell();
            col6.CellTemplate = cel6;
            col6.HeaderText = "货物";
            oneOrderDataview.Columns.Add(col6);
            DataGridViewColumn col7 = new DataGridViewColumn();
            DataGridViewCell cel7 = new DataGridViewTextBoxCell();
            col7.CellTemplate = cel7;
            col7.HeaderText = "数量";
            oneOrderDataview.Columns.Add(col7);
            DataGridViewColumn col8 = new DataGridViewColumn();
            DataGridViewCell cel8 = new DataGridViewTextBoxCell();
            col8.CellTemplate = cel8;
            col8.HeaderText = "单价";
            oneOrderDataview.Columns.Add(col8);




        }

        private void add_order_click(object sender, EventArgs e)
        {
            addOrderForm temp = new addOrderForm();
            temp.myOrderService = this.myOrderService;
            temp.ShowDialog();
            newAdderForm = temp;
            this.myOrderService = newAdderForm.myOrderService;
            allOrderView.Rows.Clear();
            refreshOrdersData();
        }

        private void refreshOrdersData()
        {
            int nums_of_orders = myOrderService.order.ToArray().Length;
            for (int i = 0; i < nums_of_orders; i++)
            {
                List<string> values = new List<string>();
                values.Add(myOrderService.order[i].id.ToString());
                values.Add(myOrderService.order[i].customer);
                values.Add(myOrderService.order[i].Date);
                values.Add(myOrderService.order[i].Money.ToString());
                allOrderView.Rows.Add(values.ToArray());
            }
        }

        private void delete_one_order_click(object sender, EventArgs e)
        {
            int deleteId =int.Parse(allOrderView.CurrentRow.Cells[0].Value.ToString());
            allOrderView.Rows.Remove(allOrderView.CurrentRow);
            this.myOrderService.removeOrder(deleteId);
        }

        private void editOrder_action(object sender, EventArgs e)
        {
            OrderService tempmyOrderService = this.myOrderService;
            editOrdermessage temp = new editOrdermessage(tempmyOrderService, allOrderView.CurrentRow.Cells[0].Value.ToString());
            temp.ShowDialog();
            neweditForm = temp;
            this.myOrderService = neweditForm.myOrderService;
            int deleteId = int.Parse(allOrderView.CurrentRow.Cells[0].Value.ToString());
            allOrderView.Rows.Clear();
            this.myOrderService.removeOrder(deleteId);
            refreshOrdersData();
        }

        private void searchOrder_click(object sender, EventArgs e)
        {
            int searchid = int.Parse(allOrderView.CurrentRow.Cells[0].Value.ToString());
            Order serachorder = this.myOrderService.order.Find((item) =>
            {
                return item.id == searchid;
            });
            int nums_of_search_order_item = serachorder.orderitems.ToArray().Length;
            oneOrderDataview.Rows.Clear();
            for (int i = 0; i < nums_of_search_order_item; i++)
            {
                List<string> values = new List<string>();
                values.Add((i+1).ToString());
                values.Add(serachorder.orderitems[i].Name);
                values.Add(serachorder.orderitems[i].Number.ToString());
                values.Add(serachorder.orderitems[i].Price.ToString());
                oneOrderDataview.Rows.Add(values.ToArray());
             
            }
        }
    }
}
